var t_J_cz_h = 50;//操作选项高度
var t_J_tw_h = 112;//图文高度

var mor_wz = "点这里添加文字";         		 	//默认文字
var mor_img_tw = "images/pic_160.png";  		//默认图文封面

var daglist = "t_J_wzcz";						//最外层框
var databk = "wak"; 							//每条数据
var dag_top = "J_cz_top"; 						//上移箭头
var dag_bot = "J_cz_bot"; 						//下移箭头
var dag_clo = "J_cz_clo"; 						//删除
var dag_cz = "t_J_cz"; 							//操作项
var dag_tw = "t_J_tw"; 							//图文项
var dag_tjtp = "J_tj_tw"; 						//添加图文
var dag_tw_wz = "J_wz"; 						//图文项-文字
var dag_tw_tp = "J_tp"; 						//图文项-图片

var t_tw_gs = 0; //图文个数
var t_cz_gs = 0; //操作选项个数
var t_J_wzcz_hig = 536; //总高度(需要初始化)
//----------------------------------------------------
//文字颜色判断
function color_wz(){
	var $this = $("." + dag_tw_wz);
	$this.each(function(){
		var txt = $(this).html()
		
		if(txt != "" && txt != mor_wz){
			$(this).addClass("on")
		}
		else{
			$(this).removeClass("on")
		}
	});
}
//----------------------------------------------------
//删除数据
function data_clo(ans){
	$(document).on('click', ans, function(){
		var top_ys = $(this).parents("." + databk).css("top")//操作选项本身的top
		var reg = /\d+/; //正则表达式
		var num = top_ys.match(reg)[0]; //去掉px
		var sum_tw = num - (-t_J_cz_h); //为当前操作选项后面图文数据top赋
				
		//删除自己之后的第一个操作选项
		$(this).parents("." + databk).siblings("." + databk).each(function(){		
			var top_ys_q = $(this).css("top")//操作选项本身的top
			var reg_q = /\d+/; //正则表达式
			var num_q = top_ys_q.match(reg_q)[0]; //去掉px
			if(num_q-num == t_J_tw_h){
				$(this).remove();
			}
			//自己位置下面的元素top重新赋值
			else if(num_q-num > 0 && num_q-num != t_J_tw_h){
				$(this).css("top",num_q - t_J_cz_h - t_J_tw_h + "px");
			}
		});
		
		$(this).parents("." + databk).remove();//删除自己
		daglist_hig()//计算赋值父类高度
		clo_jt();//调用去掉上下箭头
	})	
}
//----------------------------------------------------
//添加数据
function data_tj(type,data){
	var $this = data.parents("." + databk);
	var txt = mor_wz;
	var url = '';
	var url_mus = '';
	var tw_img = mor_img_tw;
	var top_ys = $this.css("top")//操作选项本身的top
	var reg = /\d+/; //正则表达式
	var num = top_ys.match(reg)[0]; //去掉px
	var sum_tw = num - (-t_J_cz_h); //为当前操作选项后面图文数据top赋值
	var sum_cz = num -(-t_J_cz_h)-(-t_J_tw_h); //为当前操作选项后面操作数据top赋值
		
	each_cz_sx($this,num)//操作按钮赋值top	
	
	//定义图文内容
	var t_J_tw_sj = '<div class="wak t_J_tw" style="position:absolute; top:'+sum_tw+'px;" data-type="1"><input type="text" value="'+sum_tw+'" hidden ><div class="tpzd"><div class="nk"><input type="text" class="J_tp" hidden ><div class="tp J_tp" data-img="'+tw_img+'" style="background-image:url('+tw_img+')"></div><textarea class="J_wz" hidden>'+txt+'</textarea><pre class="wz J_wz">'+txt+'</pre></div><div class="caoz cz_top J_cz_top"><i class="icon-arrow"></i></div><div class="caoz cz_bot J_cz_bot"><i class="icon-arrow"></i></div><div class="caoz cz_clo J_cz_clo"><i class="icon-trash"></i></div></div></div>';
	//定义操作按钮
	var t_J_cz_sj = '<div class="wak t_J_cz" style="position:absolute; top:'+sum_cz+'px;"><div class="tanj J_tjan"><div class="tjd tj_1"><a href="javascript:;" class="ico J_tj_tw"><i class="icon-plus"></i></a></div></div></div>'
	
	$this.after(t_J_cz_sj,t_J_tw_sj)
	daglist_hig()//计算赋值父类高度
	clo_jt();//调用去掉上下箭头
}
//----------------------------------------------------	
//遍历输出数据
function data_bl(box,type,top,img,txt,url){
	var tw_img = img; 
	var txts = txt;
	var sum_tw = top; //图文数据top赋值
	var sum_cz = top + t_J_tw_h; //为当前操作选项top赋值
	
	//判断图片为空
	if(img == ''){
		tw_img = mor_img_tw;
	}

	//文字为空
	if(txts == ""){
		txts = mor_wz;
	}
	
	//定义图文内容
	var t_J_tw_sj = '<div class="wak t_J_tw" style="position:absolute; top:'+sum_tw+'px;" data-type="1"><input type="text" value="'+top+'" hidden ><div class="tpzd"><div class="nk"><input type="text" value="'+img+'" class="J_tp" hidden ><div class="tp J_tp" data-img="'+tw_img+'" style="background-image:url('+tw_img+')"></div><textarea class="J_wz" hidden>'+txt+'</textarea><pre class="wz J_wz">'+txts+'</pre></div><div class="caoz cz_top J_cz_top"><i class="icon-arrow"></i></div><div class="caoz cz_bot J_cz_bot"><i class="icon-arrow"></i></div><div class="caoz cz_clo J_cz_clo"><i class="icon-trash"></i></div></div></div>';
	//定义操作按钮		
	var t_J_cz_sj = '<div class="wak t_J_cz" style="position:absolute; top:'+sum_cz+'px;"><div class="tanj J_tjan"><div class="tjd tj_1"><a href="javascript:;" class="ico J_tj_tw"><i class="icon-plus"></i></a></div></div></div>'
	
	box.append(t_J_tw_sj,t_J_cz_sj)
	color_wz() //文字颜色
	daglist_hig()//计算赋值父类高度
	clo_jt();//调用去掉上下箭头
}
//----------------------------------------------------
//每一个元素赋值top
function databk_top(){
	var total_height = 0;
	$('.' + databk).css("position","absolute")
	$.each($('.' + databk), function(index, item){
		$(item).css("top",total_height);
		if($(this).hasClass(dag_cz)){
			total_height = total_height + t_J_cz_h;
		} else {
			total_height = total_height + t_J_tw_h;
		}
	});
}
//----------------------------------------------------
//去掉第一个和最后一个的对应箭头
function clo_jt(){
	$("." + databk).each(function(){		
		var zong_gs = $("." + dag_tw).length; //遍历出图文的个数
		var J_cz_bot_zhtop = t_J_wzcz_hig - t_J_cz_h - t_J_tw_h; //找出最后一个操作元素
		var $this_t = $(this);
		if(zong_gs == 1){
			$this_t.find("."+dag_top).css("display","none");
			$this_t.find("."+dag_bot).css("display","none");
		}
		else if($this_t.css("top") == t_J_cz_h + "px" && zong_gs == 2){
			$this_t.find("."+dag_top).css("display","none");
			$this_t.find("."+dag_bot).css("display","block");
		}
		else if($this_t.css("top") == J_cz_bot_zhtop + "px" && zong_gs == 2){
			$this_t.find("."+dag_top).css("display","block");
			$this_t.find("."+dag_bot).css("display","none");
		}
		else if($this_t.css("top") == t_J_cz_h + "px"){
			$this_t.find("."+dag_top).css("display","none");
		}
		else if($this_t.css("top") == J_cz_bot_zhtop + "px"){
			$this_t.addClass('on');
			$this_t.find("."+dag_bot).css("display","none");
		}
		else{
			$this_t.find("."+dag_top).css("display","block");
			$this_t.find("."+dag_bot).css("display","block");
		}
	});
}
//----------------------------------------------------
//遍历操作按钮赋值top
function each_cz_sx(cz_class,cz_top) {
	//向下赋值操作
	cz_class.nextAll("." + databk).each(function(){		
		var top_ys_x = $(this).css("top")//操作选项本身的top
		var reg_x = /\d+/; //正则表达式
		var num_x = top_ys_x.match(reg_x)[0]; //去掉px
		var sum_x = num_x-(-t_J_cz_h)-(-t_J_tw_h); //为当前操作选项后面top赋值
		if(num_x-cz_top > 0 ){
			$(this).css("top",sum_x+"px");
		}
	});
	//向上赋值操作
	cz_class.prevAll("." + databk).each(function(){		
		var top_ys_s = $(this).css("top")//操作选项本身的top
		var reg_s = /\d+/; //正则表达式
		var num_s = top_ys_s.match(reg_s)[0]; //去掉px
		var sum_s = num_s-(-t_J_cz_h)-(-t_J_tw_h); //为当前操作选项后面top赋值
		if(num_s-cz_top > 0 ){
			$(this).css("top",sum_s+"px");
		}
	});
}
//----------------------------------------------------
//计算并赋值父级高度
function daglist_hig(cz_class,cz_top) {
	t_tw_gs = $("." + dag_tw).length; //计算图文个数
	t_cz_gs = $("." + dag_cz).length; //计算操作个数
	t_J_wzcz_hig = t_tw_gs*t_J_tw_h + t_cz_gs*t_J_cz_h; //计算父级高度
	$("."+daglist).height(t_J_wzcz_hig); //父级高度赋值
}
//----------------------------------------------------
//点击上移
$(document).on('click', '.'+ dag_top, function(){
	var sh = t_J_cz_h + t_J_tw_h;
	var str = $(this).parents("." + databk).css("top");//自己的top
	var reg = /\d+/; //正则表达式
	var num = str.match(reg)[0]; //去掉px
	var top_acv = num-sh; //自己移动后的top
	//获取上一个并且赋值
	$("."+databk).each(function(){
		if($(this).css("top") == top_acv + "px"){
			$(this).css("top",top_acv+sh); 
		 }
	});
	$(this).parents("." + databk).css("top",top_acv); //赋值给自己
	clo_jt();//调用去掉上下箭头
});
//点击下移
$(document).on('click', '.'+ dag_bot, function(){
	var sh = t_J_cz_h + t_J_tw_h;
	var str = $(this).parents("." + databk).css("top");//自己的top
	var reg = /\d+/; //正则表达式
	var num = str.match(reg)[0]; //去掉px
	var top_acv = num-(-sh); //自己移动后的top
	//获取下一个并且赋值
	$("."+databk).each(function(){
		if($(this).css("top") == top_acv + "px"){
			 $(this).css("top",top_acv-sh);
		 }
	});
	$(this).parents("." + databk).css("top",top_acv); //赋值给自己
	clo_jt();//调用去掉上下箭头
});